#include "drv_CC1101.h"

 const uint8_t PaTabel[ ] = PA_TABLE;
// Sync word qualifier mode = 30/32 sync word bits detected 
// CRC autoflush = true 
// Channel spacing = 199.951172 
// Data format = Normal mode 
// Data rate = 2.00224 
// RX filter BW = 58.035714 
// PA ramping = false 
// Preamble count = 4 
// Whitening = false 
// Address config = Address config = Address check and 0 (0x00) and 255 (0xFF) broadcast 
// Carrier frequency = 432.999817 
// Device address = 0 
// TX power = 10 
// Manchester enable = false 
// CRC enable = true 
// Deviation = 5.157471 
// Packet length mode = Variable packet length mode. Packet length configured by the first byte after sync word 
// Packet length = 255 
// Modulation format = GFSK 
// Base frequency = 432.999817 
// Modulated = true 
// Channel number = 0 
// PA table
static const uint8_t CC1101InitData[ 22 ][ 2 ]= 
{
    { CC1101_IOCFG0,      0x07 },
    { CC1101_FIFOTHR,     0x47 },
    { CC1101_PKTLEN,      0x3C },
    { CC1101_PKTCTRL1,    0x0F },
    { CC1101_FSCTRL1,     0x06 },
    { CC1101_FREQ2,       0x10 },
    { CC1101_FREQ1,       0xA7 },
    { CC1101_FREQ0,       0x62 },
    { CC1101_MDMCFG4,     0xF6 },
    { CC1101_MDMCFG3,     0x43 },
    { CC1101_MDMCFG2,     0x13 },
    { CC1101_DEVIATN,     0x15 },
    { CC1101_MCSM0,       0x18 },
    { CC1101_FOCCFG,      0x16 },
    { CC1101_WORCTRL,     0xFB },
    { CC1101_FSCAL3,      0xE9 },
    { CC1101_FSCAL2,      0x2A },
    { CC1101_FSCAL1,      0x00 },
    { CC1101_FSCAL0,      0x1F },
    { CC1101_TEST2,       0x81 },
    { CC1101_TEST1,       0x35 },
    { CC1101_TEST0,       0x09 },
};

//CC1101写命令
void CC1101_Write_Cmd( uint8_t Command )
{
    CC1101_SET_CSN_LOW( );                  //SPI片选，本工程中该函数都是用作SPI片选
    drv_spi_read_write_byte( Command );     //写命令
    CC1101_SET_CSN_HIGH( );                 //SPI取消片选，本工程中该函数都是用作取消SPI片选
}

//CC1101写一个寄存器
void CC1101_Write_Reg( uint8_t Addr, uint8_t WriteValue )
{
    CC1101_SET_CSN_LOW( );                  //SPI片选
    drv_spi_read_write_byte( Addr );        //写地址
    drv_spi_read_write_byte( WriteValue );  //写数据
    CC1101_SET_CSN_HIGH( );                 //取消片选
}

//CC1101连续写多个寄存器
void CC1101_Write_Multi_Reg( uint8_t Addr, uint8_t *pWriteBuff, uint8_t WriteSize )
{
    uint8_t i;

    CC1101_SET_CSN_LOW( );
    drv_spi_read_write_byte( Addr | WRITE_BURST );      //连续写命令 及首地址
    for( i = 0; i < WriteSize; i++ )
    {
        drv_spi_read_write_byte( *( pWriteBuff + i ) ); //连续写入数据
    }
    CC1101_SET_CSN_HIGH( );
}

//CC1101读一个寄存器
uint8_t CC1101_Read_Reg( uint8_t Addr )
{
    uint8_t l_RegValue = 0;
    CC1101_SET_CSN_LOW( );
    drv_spi_read_write_byte( Addr | READ_SINGLE );          //单独读命令 及地址
    l_RegValue = drv_spi_read_write_byte( 0xFF );           //读取寄存器,0xFF可以随便是什么。
    CC1101_SET_CSN_HIGH( );
    return l_RegValue;
}

//CC1101连续读多个寄存器
void CC1101_Read_Multi_Reg( uint8_t Addr, uint8_t *pReadBuff, uint8_t ReadSize )
{
    uint8_t i = 0, j = 0;
    CC1101_SET_CSN_LOW( );
    drv_spi_read_write_byte( Addr | READ_BURST );
    for( i = 0; i < ReadSize; i ++ )
    {
        for( j = 0; j < 20; j ++ );
        *( pReadBuff + i ) = drv_spi_read_write_byte( 0xFF );
    }
    CC1101_SET_CSN_HIGH( );
}

//CC1101读状态寄存器，所有状态寄存器都是只读的，地址与命令共用，通过BURST来区分
uint8_t CC1101_Read_Status( uint8_t Addr )
{
    uint8_t l_RegStatus = 0;
    CC1101_SET_CSN_LOW( );
    drv_spi_read_write_byte( Addr | READ_BURST );
    l_RegStatus = drv_spi_read_write_byte( 0xFF );
    CC1101_SET_CSN_HIGH( );
    return l_RegStatus;
}

//设置地址和地址模式
void CC1101_Set_Address_Mode(uint8_t AddressMode)
{
    uint8_t btmp;
    btmp = CC1101_Read_Reg( CC1101_PKTCTRL1 ) & ( ~0x03 );  //读出,并清空低2位
    btmp |= ( AddressMode & 0x03 );                         //设置参数到低2位
    CC1101_Write_Reg( CC1101_PKTCTRL1, btmp);               //写入寄存器
}

//CC1101初始化WOR功能
void C1101_WOR_Init( void )
{
    CC1101_Write_Reg(CC1101_MCSM0,0x18);
    CC1101_Write_Reg(CC1101_WORCTRL,0x78); 
    CC1101_Write_Reg(CC1101_MCSM2,0x00);
    CC1101_Write_Reg(CC1101_WOREVT1,0x8C);
    CC1101_Write_Reg(CC1101_WOREVT0,0xA0);
    CC1101_Write_Cmd( CC1101_SWORRST );
}

//CC1101复位
void CC1101_Reset( void )
{
    CC1101_SET_CSN_HIGH( );
    CC1101_SET_CSN_LOW( );
    CC1101_SET_CSN_HIGH( );
    delay_us( 40 );
    CC1101_Write_Cmd( CC1101_SRES );
}

//CC1101初始化
void CC1101_Init( void )
{
    uint8_t i = 0;
    CC1101_Reset( );
    //写入配置参数
    for( i = 0; i < 22; i++ )
    {
        CC1101_Write_Reg( CC1101InitData[i][0], CC1101InitData[i][1] );
    }
    //写入PA Table
    CC1101_Write_Multi_Reg( CC1101_PATABLE, (uint8_t*)PaTabel, 8 );
}

//发送包
void CC1101_Tx_Packet( uint8_t address, uint8_t TxSize, uint8_t *pTxBuff )
{
    CC1101_Clear_TxBuffer;
    CC1101_Write_Reg( CC1101_TXFIFO, TxSize + 1);               //写入长度
    CC1101_Write_Reg( CC1101_TXFIFO, address);                  //写入地址
    CC1101_Write_Multi_Reg( CC1101_TXFIFO, pTxBuff, TxSize );   //写入数据
    CC1101_Tx_Mode;                                             //发送模式
}
